import Router from 'express';



const petRouter = Router();

//添加宠物
petRouter.post('/addPet', async (req, res) => {
    const {
        name,
        varietyName,
        color,
        breed,
        sex,
        weight,
        birthday,
        isJueYU,
        isFear,
        isAttack
    } = req.body
    if (!name) return res.fail('宠物昵称不能为空')
    if (!varietyName) return res.fail('品名不能为空')
    if (!color) return res.fail('宠物颜色不能为空')
    if (!breed) return res.fail('宠物品种不能为空')
    if (!sex) return res.fail('宠物性别不能为空')
    if (!weight) return res.fail('宠物体重不能为空')
    if (!birthday) return res.fail('宠物生日不能为空')
    if (!isJueYU) return res.fail('宠物是否绝育不能为空')
    if (!isFear) return res.fail('宠物是否怕生不能为空')
    if (!isAttack) return res.fail('宠物是否会攻击人不能为空')
    try {
        const db = req.db
        const addPet = {
            name,
            varietyName,
            color,
            breed,
            sex,
            weight,
            birthday,
            isJueYU,
            isFear,
            isAttack
        }
        if (name) addPet.name = name
        await db.Pet.create({
            data: addPet
        })
        res.ok({ success: true, info: '添加成功' })
    } catch (e) {
        console.log(e);
        res.fail({ success: false, info: '添加失败' })
    }

})

//删除宠物
petRouter.post('/delPet', async (req, res) => {
    const {
        id
    } = req.body
    try {
        const db = req.db
        await db.Pet.delete({
            where: {
                id: Number(id)
            }
        })
        res.ok({ success: true, info: '删除成功' })
    } catch (e) {
        console.log(e);
        res.fail({ success: false, info: '删除失败' })
    }
})

//修改宠物信息
petRouter.post('/updatePet', async (req, res) => {
    const {
        name,
        varietyName,
        color,
        breed,
        sex,
        weight,
        birthday,
        isJueYU,
        isFear,
        isAttack
    } = req.body
    if (!name) return res.fail('宠物名称不能为空')
    try {
        const db = req.db
        const updatePet = {}
        if (name) updatePet.name = name
        if (varietyName) updatePet.varietyName = varietyName
        if (color) updatePet.color = color
        if (breed) updatePet.breed = breed
        if (sex) updatePet.sex = sex
        if (weight) updatePet.weight = weight
        if (birthday) updatePet.birthday = birthday
        if (isJueYU) updatePet.isJueYU = isJueYU
        if (isFear) updatePet.isFear = isFear
        if (isAttack) updatePet.isAttack = isAttack
        await db.Pet.update({
            data: updatePet,
            where: {
                name
            }
        })
        res.ok({ success: true, info: '修改成功' })
    } catch (e) {
        console.log(e);
        res.fail({ success: false, info: '修改失败' })
    }
})

//查询宠物信息
petRouter.post('/findPet', async (req, res) => {
    const {
        name,
        varietyName,
        color,
        breed,
        sex,
        weight,
        birthday,
        isJueYU,
        isFear,
        isAttack
    } = req.body
    try {
        const db = req.db
        const findPet = await db.Pet.findMany({
            where: {
                name,
                varietyName,
                color,
                breed,
                sex,
                weight,
                birthday,
                isJueYU,
                isFear,
                isAttack
            }
        })
        res.ok({ success: true, info: '查询成功', data: findPet })
    } catch (e) {
        console.log(e);
        res.fail({ success: false, info: '查询失败' })
    }
})





export default petRouter;